// pages/order/detail.js
const app = getApp()
Page({

  /**
   * 页面的初始数据
   */
  data: {
    dialogShow: false,// 确认对话框按钮-是否显示标志位
    buttons: [{ id: 'cancle', text: '取消' }, { id: 'pay', text: '缴费' }], // 确认对话框按钮-缴费并出库
    show: true// loading显示标志位
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var that = this;
    wx.request({
      header: {
        'content-type': 'application/x-www-form-urlencoded',
        'Authorization': 'Bearer ' + wx.getStorageSync('token')
      },
      url: getApp().data.servser + '/parking/place/use/detail',
      data: {
        id: options.id
      },
      success: function (res) {
        that.setData({
          parkingPlaceUseInfo: res.data.data.parkingPlaceUseInfo,
          parkingPlaceInfo: res.data.data.parkingPlaceInfo,
          parkingInfo: res.data.data.parkingInfo,
          parking_hour: res.data.data.parking_hour, // 停车时长
          needPayPrice: res.data.data.needPayPrice, // 停车费用
          show: false
        })
      }
    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  },
  // 缴费并出库-取消 or 缴费
  tapCarOutButton: function (e) {
    let id = e.detail.item.id
    // 取消
    if (id == 'cancle') {
      this.setData({
        dialogShow: false
      })
    }
    // 缴费
    else {
      var that = this;
      wx.request({
        header: {
          'content-type': 'application/x-www-form-urlencoded',
          'Authorization': 'Bearer ' + wx.getStorageSync('token')
        },
        url: getApp().data.servser + '/parking/place/use/car/out',
        method: 'POST',
        data: {
          parkingPlaceId: that.data.parkingPlaceInfo.seqId,
          parkingId: that.data.parkingInfo.seqId,
          openId: app.globalData.openid,
          money: that.data.needPayPrice
        },
        success: function (res) {
          wx.showToast({
            title: '缴费成功!',
            icon: 'none',
            duration: 2000,
            success: function (e) {
              wx.reLaunch({
                url: '../index/parkinglist',
              })
            }
          })
        }
      })
    }
  },
  // 缴费并出库-确认框
  carOut: function () {
    this.setData({
      dialogShow: true
    })
  },
  // 取消预约
  cancel: function () {
    var that = this;
    wx.request({
      header: {
        'content-type': 'application/x-www-form-urlencoded',
        'Authorization': 'Bearer ' + wx.getStorageSync('token')
      },
      url: getApp().data.servser + '/parking/place/use/cancel/order',
      method: 'POST',
      data: {
        parkingPlaceId: that.data.parkingPlaceInfo.seqId,
        parkingId: that.data.parkingInfo.seqId,
        openId: app.globalData.openid
      },
      success: function (res) {
        wx.showToast({
          title: '取消预约成功!',
          icon: 'none',
          duration: 2000,
          success: function () {
            wx.reLaunch({
              url: '../index/parkinglist',
            })
          }
        })
      }
    })
  }
})